package com.cjk.ssm.service.impl;

import com.cjk.ssm.entity.Department;
import com.cjk.ssm.entity.Employee;
import com.cjk.ssm.exp.MyNoDataException;
import com.cjk.ssm.exp.MyUpdFailException;
import com.cjk.ssm.mapper.EmployeeMapper;
import com.cjk.ssm.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 员工 -- 服务层实现类
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    private final int defaultPageNum = 1;//默认页码
    private final int defaultPageSize = 3;//默认每页显示条数
    private final int defalutNavigatePages = 5;//默认导航分页的页码数
    private final String findMsg = "no data";//无数据提示信息
    private final String addMsg = "save fail";//添加数据失败提示信息
    private final String delMsg = "delete fail";//删除数据失败提示信息
    private final String updMsg = "update fail";//修改数据失败提示信息
    //@Resource
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 带分页的查询所有
     *
     * @param pageNume
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<Employee> findAllPage(Integer pageNume, Integer pageSize) throws Exception {
        if (pageNume == null || pageNume < 1) {
            pageNume = this.defaultPageNum;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = this.defaultPageSize;
        }
        List<Employee> employees;
        try {
            PageHelper.startPage(pageNume, pageSize);
            employees = employeeMapper.findAll();
            if (employees == null) {
                //如果查不到数据抛出无数据异常
                throw new MyNoDataException(this.findMsg);
            }
            PageInfo<Employee> pageInfo = new PageInfo<>(employees, this.defalutNavigatePages);
            return pageInfo;
        } catch (Exception e) {
//          抛出其他类型异常
            throw e;
        }

    }

    /**
     * 添加员工信息
     *
     * @param employee
     * @throws Exception
     */
    @Override
    public void add(Employee employee) throws Exception {
        try {
            boolean add = employeeMapper.add(employee);
            if (!add) {
                throw new MyNoDataException(this.addMsg);//添加失败
            }
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 删除操作
     *
     * @param id
     * @throws Exception
     */
    @Override
    public void del(Integer id) throws Exception {
        try {
            boolean del = employeeMapper.del(id);
            if (!del) {
                throw new MyNoDataException(this.delMsg);
            }
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 根据id查找
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public Employee findById(Integer id) throws Exception {
        Employee employee;
        try {
            employee = employeeMapper.findById(id);
            if (employee == null) {
                throw new MyNoDataException(this.findMsg);
            }

        } catch (Exception e) {
            throw e;
        }
        return employee;
    }

    @Override
    public void upd(Employee employee) throws Exception {
        boolean upd;
        try {
            upd = employeeMapper.upd(employee);
            if (!upd) {
                throw new MyUpdFailException(this.updMsg);
            }
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 模糊查找分页信息
     * @param name
     * @param pageNume
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Employee> findByEmpNameLikePage(String name, Integer pageNume, Integer pageSize)throws Exception {
        PageInfo<Employee> employeePageInfo;
        if (pageNume == null || pageNume < 1) {
            pageNume = this.defaultPageNum;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = this.defaultPageSize;
        }
        try {
            PageHelper.startPage(pageNume, pageSize);
            List<Employee> employees = employeeMapper.findByEmpNameLike("%" + name + "%");
            System.out.println(""+employees);
            if (employees == null || employees.size() < 1) {
                throw new MyNoDataException(this.findMsg);
            }
//        将其封装到PageInfo对象中
            employeePageInfo= new PageInfo<>(employees, this.defalutNavigatePages);
            return employeePageInfo;
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 查找所有数据
     *
     * @return
     */
    @Override
    public List<Employee> findAll() {
        List<Employee> employees = employeeMapper.findAll();
        return employees;
    }
}
